package cn.com.okgotest.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.okgo.OkGo;
import cn.com.okgo.cache.CacheMode;
import cn.com.okgo.callback.HttpStringCallback;
import cn.com.okgotest.R;
import cn.com.okgotest.fragment.base.BaseFragment;

public class GetFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener,View.OnClickListener {

    private TextView sourceTv;
    private TextView contentTv;
    private TextView retryTv;
    private Button button;
    private CacheMode cacheMode = CacheMode.NO_CACHE;
    private List<String> baseUrls=new ArrayList<>();
    private int maxRetry;
    private String baseUrl="http://sjzqali.csc108.com:9800";
    private boolean needChangeService;

    private int sum=1;
    private StringBuffer sb=new StringBuffer();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_get, container, false);
        init(view);
        return view;
    }

    private void init(View view) {
        sourceTv = view.findViewById(R.id.sourceTv);
        contentTv = view.findViewById(R.id.contentTv);
        retryTv = view.findViewById(R.id.retryTv);

        button=view.findViewById(R.id.button);
        RadioGroup radioGroup = view.findViewById(R.id.radioGroup);
        RadioGroup radioGroup_2=view.findViewById(R.id.radioGroup_2);
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup_2.setOnCheckedChangeListener(this);
        button.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if(radioGroup.getId()==R.id.radioGroup){
            button.setText("开始请求");
            switch (i) {
                case R.id.radioButton_1:
                    cacheMode = CacheMode.NO_CACHE;
                    break;
                case R.id.radioButton_2:
                    cacheMode = CacheMode.REQUEST_FAILED_READ_CACHE;
                    break;
                case R.id.radioButton_3:
                    cacheMode = CacheMode.FIRST_CACHE_THEN_REQUEST;
                    break;
            }
        }else if(radioGroup.getId()==R.id.radioGroup_2){
            switch (i) {
                case R.id.noneRButton:
                    baseUrl="http://sjzqali.csc108.com:9800";
                    baseUrls.clear();
                    maxRetry=0;
                    needChangeService=false;
                    break;
                case R.id.siteRButton:
                    baseUrl="http://sjzqali.csc1085.com:9800";
                    baseUrls.add("http://sjzqali.csc1081.com:9800");
                    baseUrls.add("http://sjzqali.csc1082.com:9800");
                    baseUrls.add("http://sjzqali.csc1083.com:9800");
                    baseUrls.add("http://sjzqali.csc108.com:9800"); //正确地址
                    baseUrls.add("http://sjzqali.csc1084.com:9800");
                    maxRetry=0;
                    needChangeService=true;
                    break;
                case R.id.retryRButton:
                    baseUrls.clear();
                    maxRetry=3;
                    baseUrl="http://sjzqali.csc1085.com:9800";
                    needChangeService=false;
                    break;
            }
        }

    }


    @Override
    public void onClick(View view) {

        switch (cacheMode){
            case NO_CACHE:
                button.setText("请求模式: 不使用缓存");
                break;
            case REQUEST_FAILED_READ_CACHE:
                button.setText("请求模式: 请求网络失败后，读取缓存");
                break;
            case FIRST_CACHE_THEN_REQUEST:
                button.setText("请求模式: 先使用缓存，不管是否存在，仍然请求网络");
                break;
        }

        getRequest();
    }

    private void getRequest(){
        sum=1;
        sb.setLength(0);
        sourceTv.setText("数据来源");
        contentTv.setText("");
        OkGo.get("/api/tg-service/portfolio/zhlb/select/100000?userID=-1&zhlx=0&page=1&count=5")
                .baseUrl(baseUrl)
                .baseUrls(baseUrls)
                .maxRetry(maxRetry)
                .delay(3000)
                .cacheMode(cacheMode)
                .needChangeService(needChangeService)
                .execute(new HttpStringCallback() {
                    @Override
                    public void onSuccess(String obj, String baseUrl) {
                        contentTv.setTextColor(Color.BLUE);
                        sourceTv.setText("数据来源: "+"服务器");
                        contentTv.setText(obj);
                        sb.append("最终地址：").append(baseUrl);
                        retryTv.setText(sb.toString());
                    }

                    @Override
                    public void onCache(String obj,String baseUrl) {
                        contentTv.setTextColor(Color.BLUE);
                        sourceTv.setText("数据来源: "+"缓存");
                        contentTv.setText(obj);
                        sb.append("最终地址：").append(baseUrl);
                        retryTv.setText(sb.toString());
                    }

                    @Override
                    public void onFailed(Exception e, String baseUrl) {
                        contentTv.setTextColor(Color.RED);
                        contentTv.setText(e.toString());
                        sb.append("最终地址：").append(baseUrl);
                        retryTv.setText(sb.toString());
                    }

                    @Override
                    public void onRetry(String baseUrl) {
                        sb.append("重试第 "+sum+" 次 : ").append(baseUrl).append("\n");
                        retryTv.setText(sb.toString());
                        sum++;
                    }
                });
    }

}
